suite("event-bus", function () {
    "use strict";

    var EventBus = require("./event-bus");
    var assert = require("assert");

    test("no fail on no handler", function () {
        var bus = new EventBus;
        bus.on("some");
    });

    test("trigger single", function () {
        var bus = new EventBus;
        var called = false;
        bus.on("some", function () {
            called = true;
        });
        bus.trigger("some");
        assert(called);
    });

    test("trigger single with args", function () {
        var bus = new EventBus;
        var arg = 0;
        bus.on("some", function (earg) {
            arg = earg;
        });
        bus.trigger("some", 1);
        assert(arg === 1);
    });

    test("trigger multiple with multiple args", function () {
        var bus = new EventBus;
        var args = [];
        bus.on("some", function (arg0, arg1) {
            args[0] = arg0;
            args[1] = arg1;
        });
        bus.on("some", function (arg0, arg1) {
            args[2] = arg0;
            args[3] = arg1;
        });
        bus.trigger("some", 1, 2);
        assert(args[0] === 1 && args[1] === 2 &&
            args[2] === 1 && args[3] === 2);
    });

    test("off", function () {
        var bus = new EventBus;
        var called = false;
        bus.on("some", function () {
            called = true;
        });
        bus.off("some");
        assert(!called);
    });

    test("off specific", function () {
        var bus = new EventBus;
        var called = false;
        var handler = function () {
            called = true;
        };
        bus.on("some", handler);
        var othersCalled = false;
        bus.on("some", function () {
            othersCalled = true;
        });
        bus.off("some", handler);
        bus.trigger("some");
        assert(!called && othersCalled);
    });

    test("applies to single instance", function () {
        var bus0 = new EventBus;
        var bus1 = new EventBus;
        bus0.on("some", function handler() {
            assert(false);
        });
        bus1.trigger("some");
    });
});
